package com.lkpstudy.controller;

import com.lkpstudy.pojo.Result;
import com.lkpstudy.utils.AliOssUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@CrossOrigin(origins = "http://localhost:5173",allowCredentials = "true") //允许跨域
public class FileUploadController {
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        //把文件的内容存储到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        //保证文件的名字是唯一的，从而防止文件覆盖   截取原始图片文件名的最后一个'.'之后的字符串，即文件后缀名
        String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
//        file.transferTo(new File("D:\\SpringBoot\\files\\" + filename)); //存储到本地文件夹里
        String url = AliOssUtil.uploadFile(filename, file.getInputStream()); //调用AliOssUtil存储到阿里云服务器的bucket里
        return Result.success(url);
//        return Result.success("url访问地址...");
    }
}
